package com.zjsystem.zjsy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.zjsystem.zjsy.dto.ZjAction;
import com.zjsystem.zjsy.dto.ZjxdPlan;
import com.zjsystem.zjsy.dto.ZjxdWaveOrder;
import com.zjsystem.zjsy.mapper.ZjActionMapper;
import com.zjsystem.zjsy.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjsystem.zjsy.vo.ActionMonitorNumberVO;
import com.zjsystem.zjsy.vo.ActionMonitorVO;
import com.zjsystem.zjsy.vo.ActionVO;
import com.zjsystem.zjsy.vo.WaveOrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 战机行动表 服务实现类
 * </p>
 *
 * @author wyf
 * @since 2024-07-13
 */
@Service
public class ZjActionServiceImpl extends ServiceImpl<ZjActionMapper, ZjAction> implements ZjActionService {
    @Resource
    private ZjxdPlanService planService;//筹划任务表
    @Resource
    private ZjActionService actionService;//行动计划表
    @Resource
    private ZjxdWaveOrderService waveOrderService;//波次表

    @Override
    public ActionMonitorVO listView(String rwId) {
        ActionMonitorVO voResult = new ActionMonitorVO();
        ZjxdPlan plan = planService.getOne(Wrappers.<ZjxdPlan>lambdaQuery().eq(ZjxdPlan::getAssId, rwId));
        if (plan == null) {
            throw new RuntimeException("该任务还未达到【行动监控阶段】， 请先关注【筹划监控】阶段");
        }
        // 步骤一：封装弹药 通道 和机器人所需数据
        ActionMonitorNumberVO numberVO = new ActionMonitorNumberVO();
        voResult.setAmmStatus(numberVO);
        voResult.setRobotStatus(numberVO);
        voResult.setChannelStatus(numberVO);

        // 步骤二：封装行动列表数据
        List<ZjAction> actionList = actionService.list(Wrappers.<ZjAction>lambdaQuery().eq(ZjAction::getPlanId, plan.getId()));
        if (CollectionUtils.isEmpty(actionList)) {
            throw new RuntimeException("行动计划暂未生成 请耐心等待");
        }
        List<ActionVO> actionVOList = Lists.newArrayList();
        List<Long> actionIds = actionList.stream().map(ZjAction::getId).collect(Collectors.toList());

        List<ZjxdWaveOrder> waveOrderList = waveOrderService.listByIds(actionIds);

        if (CollectionUtils.isEmpty(waveOrderList)) {
            throw new RuntimeException("波次信息暂未生成 请耐心等待");
        }

        for (ZjAction zjAction : actionList) {
            ActionVO actionVO = new ActionVO();
            Long zjActionId = zjAction.getId();
            actionVO.setActionId(String.valueOf(zjActionId));
            actionVO.setActionName(zjAction.getActionName());
            // 封装波次信息
            List<WaveOrderVO> waveOrderVOList = Lists.newArrayList();
            waveOrderList.forEach(wave -> {
                WaveOrderVO waveOrderVO = new WaveOrderVO();
                if (wave.getActionId().equals(zjActionId)) {
                    BeanUtils.copyProperties(wave, waveOrderVO);
                    waveOrderVOList.add(waveOrderVO);
                }
            });
            actionVO.setWaveOrderVOList(waveOrderVOList);
            actionVOList.add(actionVO);
        }
        voResult.setActionList(actionVOList);
        return voResult;
    }

}
